package com.example.client_anna.util.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.example.client_anna.R;
import com.example.client_anna.network.okhttp3.RequestUrl;
import com.example.client_anna.util.entity.OrderGoods;

import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * @author cy
 * @email 272884043@qq.com
 * @date 2021/11/8
 */
public class OrderCoreAdapter extends RecyclerView.Adapter<OrderCoreAdapter.ViewHolder>  {

    private Context mContext;
    private List<OrderGoods> mGoods;

    //viewholder与xml布局中的控件关联
    static class ViewHolder extends RecyclerView.ViewHolder{
        ImageView order_img;
        TextView order_title;

        public ViewHolder(View view){
            super(view);
            order_img = (ImageView)view.findViewById(R.id.order_core_img);
            order_title = (TextView)view.findViewById(R.id.order_core_title);
        }
    }

    //为适配器传入数据
    public OrderCoreAdapter(List<OrderGoods> goods) {
        mGoods = goods;
    }

    @NonNull
    @NotNull
    @Override
    public OrderCoreAdapter.ViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup viewGroup, int viewType) {
        if (mContext == null) {
            mContext = viewGroup.getContext();
        }
        View view = LayoutInflater.from(mContext).inflate(R.layout.order_interface_core, viewGroup, false);
        ViewHolder holder = new ViewHolder(view);

        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull OrderCoreAdapter.ViewHolder holder, int position) {
        OrderGoods orderGoods = mGoods.get(position);
        holder.order_title.setText(orderGoods.getFood_name());

        String url = RequestUrl.getUrl()+orderGoods.getFood_image();
        displayImageCenter(mContext,holder.order_img,url);

    }

    public static void displayImageCenter(Context context,final ImageView imageView,String url){
        RequestOptions options = new RequestOptions()
                //磁盘缓存
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                //内存缓存，默认false，表示不跳过内存缓存
                .skipMemoryCache(false)
                //加载错误显示的图片
                .error(R.drawable.explore_error)
                //占位图
                .placeholder(R.drawable.explore_placeholder)
                //设置图片的宽和高，获取imageView控件的宽度和高
                //注：在onCreate中是获取不到imageview的高度的，因为这个时候imageView控件自己都还没有测量
                .override(imageView.getMeasuredWidth(), imageView.getMeasuredHeight())
                //不需要动画效果，默认300
                .dontAnimate();
        //Glide缓存顺序：内存>磁盘>网络
        Glide.with(context)
                .asBitmap()
                .load(url)
                .apply(options)
                .into(imageView);
    }

    @Override
    public int getItemCount() {
        return mGoods.size();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
}
